package momo.multitree.util;

import momo.multitree.structure.Edge;
import momo.multitree.structure.Graph;
import momo.multitree.structure.Node;
import momo.multitree.structure.Tree;
import junit.framework.TestCase;

public class WeightCalculatorTest extends TestCase {
	
	private Tree tree;
	private Graph graph;
	
	private Node a, b, c, d, e;
	private Edge tAB, tAC, tCD, tCE, eAD, eAE, eBD, eBE, eDE;
	
	protected void setUp() throws Exception {
		super.setUp();
		a = new Node("1", 0.2, 0.2);
		b = new Node("2", 0.3, 0.1);
		c = new Node("3", 0.1, 0.4);
		d = new Node("4", 0.25, 0.5);
		e = new Node("5", 0.15, 0.7);
		tAB = new Edge("1", a, b, 1, 1, 2, 2);
		tAC = new Edge("2", a, c, 2, 2, 4, 4);
		tCD = new Edge("3", c, d, 4, 4, 7, 7);
		tCE = new Edge("4", c, e, 6, 6, 1, 1);
		eAD = new Edge("5", a, d, 7, 7, 3, 3);
		eAE = new Edge("6", a, e, 3, 3, 9, 9);
		eBD = new Edge("7", b, d, 5, 5, 2, 2);
		eBE = new Edge("8", b, e, 9, 9, 5, 5);
		eDE = new Edge("9", d, e, 2, 2, 2, 2);
		
		graph = new Graph();
		graph.add(tAB);
		graph.add(tAC);
		graph.add(tCD);
		graph.add(tCE);
		graph.add(eAD);
		graph.add(eAE);
		graph.add(eBD);
		graph.add(eBE);
		graph.add(eDE);
		
		tree = new Tree(a);
		tree.add(tAB);
		tree.add(tAC);
		tree.add(tCD);
		tree.add(tCE);
		
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
		
	public void testGetMaxLat() {
		WeightCalculator cal = new WeightCalculator(tree, graph);
		assertEquals(9, cal.getMaxLat(), 0.001);
	}

	public void testGetMaxCost() {
		WeightCalculator cal = new WeightCalculator(tree, graph);
		assertEquals(9, cal.getMaxCost(), 0.001);
	}

	public void testGetMaxStab() {
		WeightCalculator cal = new WeightCalculator(tree, graph);
		assertEquals(0.7, cal.getMaxStab(), 0.001);
	}
	
	public void testGetWeight() {
		WeightCalculator cal = new WeightCalculator(tree, graph);
		assertEquals(0.4656, cal.getWeight(eAD, a), 0.001);
	}
	
	public void testCalWeight() {
		WeightCalculator cal = new WeightCalculator(tree, graph);
		double w_1_2 = cal.getWeight(tAB, a);
		double w_2_1 = cal.getWeight(tAB, b);
		
		double w_1_3 = cal.getWeight(tAC, a);
		double w_3_1 = cal.getWeight(tAC, c);
		
		double w_3_4 = cal.getWeight(tCD, c);
		double w_4_3 = cal.getWeight(tCD, d);
		
		double w_3_5 = cal.getWeight(tCE, c);
		double w_5_3 = cal.getWeight(tCE, e);
		
		double r2 = b.getWorkloadRate();
		double r3 = c.getWorkloadRate();
		double r4 = d.getWorkloadRate();
		double r5 = e.getWorkloadRate();
		
		double downlink_weight = w_1_2 + w_1_3 + w_3_4 + w_3_5;
		double uplink_weight = r2 * w_2_1 +
								r3 * w_3_1 +
								r4 * ( w_4_3 + w_3_1 ) +
								r5 * ( w_5_3 + w_3_1 );
		double w = downlink_weight + uplink_weight;
		double calWeight = cal.calWeight();
		assertEquals(w, calWeight, 0.001);
	}

}
